package com.qiyuesuo.server.dao;

import com.qiyuesuo.server.PO.FileInfo;
import com.qiyuesuo.server.util.DbUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 处理文件信息 fileInfo
 *
 * @author s77
 * @create 2020-4月-01 11:49
 */
public class FileInfoDao {

    private final static Logger logger = LoggerFactory.getLogger(FileInfoDao.class);

    /*
     * 批量插入list列表中的文件信息数据
     * */
    public void batchInsert(List<FileInfo> list) {
        Connection conn = null;
        PreparedStatement pstm = null;
        ResultSet rt = null;
        try {
            conn = DbUtil.getConnection();

            String sql = "INSERT INTO t_file_info values(?,?,?,?,?,?,?)";
            pstm = conn.prepareStatement(sql);

            conn.setAutoCommit(false);
            for (FileInfo fileInfo : list) {
                pstm.setString(1, fileInfo.getName());
                pstm.setLong(2, fileInfo.getSize());
                pstm.setString(3, fileInfo.getType());
                pstm.setString(4, fileInfo.getOriginalName());
                pstm.setDate(5, new Date(fileInfo.getCreationTime().getTime()));
                pstm.setString(6, fileInfo.getDirectoryAddress());
                pstm.setString(7, fileInfo.getDigitalEnvelope());
                pstm.addBatch();
                logger.info(fileInfo.getName() + " -- 文件信息加入插入数据库序列");
            }
            pstm.executeBatch();
            conn.commit();
            logger.info("文件信息批量插入");
        } catch (Exception e) {
            logger.error("文件信息插入数据库失败", e);
            throw new RuntimeException(e);
        } finally {
            if (pstm != null) {
                try {
                    pstm.close();
                } catch (SQLException e) {
                    logger.error("PreparedStatement 关闭失败", e);
                    throw new RuntimeException(e);
                }
            }
            DbUtil.closeConnection(conn);
        }
    }

    /*
     * 按照uuid查询文件信息数据
     * */
    public FileInfo select(String name) {
        Connection conn = null;
        PreparedStatement pstm = null;
        ResultSet rt = null;
        try {
            conn = DbUtil.getConnection();

            String sql = "select * from t_file_info where name = ?";
            pstm = conn.prepareStatement(sql);

            pstm.setString(1, name);

            rt = pstm.executeQuery();
            if (rt.next()) {
                FileInfo fileInfo = new FileInfo();
                fileInfo.setName(rt.getString(1));
                fileInfo.setSize(rt.getLong(2));
                fileInfo.setType(rt.getString(3));
                fileInfo.setOriginalName(rt.getString(4));
                fileInfo.setCreationTime(rt.getDate(5));
                fileInfo.setDirectoryAddress(rt.getString(6));
                fileInfo.setDigitalEnvelope(rt.getString(7));
                return fileInfo;
            } else {
                logger.error(name + " -- 未查询到文件信息");
                return null;
            }


        } catch (Exception e) {
            logger.error(name + " -- 查询文件信息失败");
            throw new RuntimeException(e);
        } finally {
            if (pstm != null) {
                try {
                    pstm.close();
                } catch (SQLException e) {
                    logger.error("PreparedStatement 关闭失败", e);
                    throw new RuntimeException(e);
                }
            }
            DbUtil.closeConnection(conn);
        }
    }

    /*
     * 查询最新的n条数据
     * */
    public List<FileInfo> selectNew(int n) {
        List<FileInfo> list = new ArrayList<>();
        Connection conn = null;
        PreparedStatement pstm = null;
        ResultSet rt = null;
        try {
            conn = DbUtil.getConnection();
            String sql = "select * from t_file_info order by creationTime desc offset 0 rows fetch next ? rows only";
            pstm = conn.prepareStatement(sql);

            pstm.setInt(1, n);

            rt = pstm.executeQuery();
            while (rt.next()) {
                FileInfo fileInfo = new FileInfo();
                fileInfo.setName(rt.getString(1));
                fileInfo.setSize(rt.getLong(2));
                fileInfo.setType(rt.getString(3));
                fileInfo.setOriginalName(rt.getString(4));
                fileInfo.setCreationTime(rt.getDate(5));
                fileInfo.setDirectoryAddress(rt.getString(6));
                fileInfo.setDigitalEnvelope(rt.getString(7));
                list.add(fileInfo);
            }


        } catch (Exception e) {
            logger.error(" -- 查询最新上传文件信息失败");
            throw new RuntimeException(e);
        } finally {
            if (pstm != null) {
                try {
                    pstm.close();
                } catch (SQLException e) {
                    logger.error("PreparedStatement 关闭失败", e);
                    throw new RuntimeException(e);
                }
            }
            DbUtil.closeConnection(conn);
        }
        return list;

    }


}
